package bar.dark.lee.alarm.effect.shake;

import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import bar.dark.lee.alarm.R;
import bar.dark.lee.alarm.effect.EffectActivity;
import bar.dark.lee.alarm.effect.EffectListActivity;
import bar.dark.lee.alarm.effect.question.QuestionResultDialog;

public class EffectShakeActivity extends EffectActivity implements SensorEventListener {

	private int effectNumber;
	private boolean flagTestReal;
	
	private TextView textCorrect;
	private Button btnSend;
	private ProgressBar progressGage;
	
	private int result;
	private int correctCount = 100;
	
	private boolean flagProgress = true;
	
	
	private long lastTime; 
    private float speed; 
    private float lastX; 
    private float lastY; 
    private float lastZ; 
 
    private float x, y, z; 
    private static final int SHAKE_THRESHOLD = 1000; 
 
    private static final int DATA_X = SensorManager.DATA_X; 
    private static final int DATA_Y = SensorManager.DATA_Y; 
    private static final int DATA_Z = SensorManager.DATA_Z; 

    private SensorManager sensorManager; 
    private Sensor accelerormeterSensor; 
    
    
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.effectshake_activity);
		
		Intent intent = getIntent();
		effectNumber = intent.getIntExtra(EffectListActivity.EFFECT_PLAY, -1);
		flagTestReal = intent.getBooleanExtra(EffectActivity.FLAG_TEST_REAL, false);
		
		if (flagTestReal)
		{
			TextView leftButton = (TextView) findViewById(R.id.superLeftButton);
			leftButton.setVisibility(View.INVISIBLE);
		}
		
		if (effectNumber == -1)
		{
			
		}
		else
		{
			super.setTitle(effectNumber);
		}
		
		setFindView();
		setDataSetting();
		setClickListener();
		
		startProgressHandler();
		
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
        accelerormeterSensor = sensorManager 
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
		
	}
	
	@Override
	public void setFindView() {
		
		textCorrect = (TextView) findViewById(R.id.effectShake_textCorrect);
		progressGage = (ProgressBar) findViewById(R.id.effectShake_progress);
		
	}

	@Override
	public void setDataSetting() {
		
		textCorrect.setText(0+"%");
		progressGage.setMax(correctCount);
		progressGage.setProgress(0);
		
	}

	@Override
	public void setClickListener() {
		
	}

	@Override
	protected void tipButton() {
	}
	
	private Handler handler = new Handler()
	{
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			
			int currentProgress = progressGage.getProgress();

			if (currentProgress > 99)
			{
			}
			else
			{
				if (flagProgress)
				{
					if (currentProgress <= 0)
					{
						progressGage.setProgress(0);
						textCorrect.setText("0%");
					}
					else
					{
						progressGage.setProgress(currentProgress - 1);
						textCorrect.setText((currentProgress - 1)+"%");
					}
					
					startProgressHandler();
				}
				else
				{
					progressGage.setProgress(100);
					textCorrect.setText("100%");
				}
			}
		}
	};
	
	private void startProgressHandler()
	{
		handler.sendEmptyMessageDelayed(0, 5 * 100);
	}
	
	private void playResult(boolean result)
	{
		Intent intent = new Intent(superContext, QuestionResultDialog.class);
		intent.putExtra(QuestionResultDialog.QUESTION_RESULT, result);
		startActivityForResult(intent, QuestionResultDialog.REQUEST_BUTTONRUSH);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == QuestionResultDialog.REQUEST_BUTTONRUSH)
		{
			if (resultCode == RESULT_OK)
			{
				finish();
			}
		}
	}
    
	@Override 
    public void onStart() { 
        super.onStart(); 

        if (accelerormeterSensor != null) 
            sensorManager.registerListener(this, accelerormeterSensor, 
                    SensorManager.SENSOR_DELAY_GAME); 
    } 

    @Override 
    public void onStop() { 
        super.onStop(); 
        if (sensorManager != null) 
            sensorManager.unregisterListener(this); 
    } 
 
    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 

    } 
 
    @Override 
    public void onSensorChanged(SensorEvent event) { 

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { 
            long currentTime = System.currentTimeMillis(); 
            long gabOfTime = (currentTime - lastTime); 
 
            if (gabOfTime > 100) { 
            	
                lastTime = currentTime; 
 
                x = event.values[SensorManager.DATA_X]; 
                y = event.values[SensorManager.DATA_Y]; 
                z = event.values[SensorManager.DATA_Z]; 

                speed = Math.abs(x + y + z - lastX - lastY - lastZ) /
                        gabOfTime * 10000;
 
                if (speed > SHAKE_THRESHOLD) { 
                    // 이벤트 발생!! 
                	int currentProgress = progressGage.getProgress();
    				int resultProgress = currentProgress + 1;
    				
    				if (flagProgress)
    				{
    					if (resultProgress >= 99)
        				{
        					flagProgress = false;
        					
        					progressGage.setProgress(100);
        					textCorrect.setText("100%");
        					
        					playResult(true);
        				}
        				else
        				{
        					if (currentProgress <= 0)
        					{
        						progressGage.setProgress(1);
        						textCorrect.setText("1%");
        					}
        					else
        					{
        						progressGage.setProgress(resultProgress);
        						textCorrect.setText(resultProgress+"%");
        					}
        				}
    				}
                } 

                lastX = event.values[DATA_X]; 
                lastY = event.values[DATA_Y]; 
                lastZ = event.values[DATA_Z]; 
            } 
        } 
    }   
}
